package ownserver;

import java.nio.ByteBuffer;

import base.Ship;


import orders.*;
import base.OPCODES;

public class Messages {
	public static ByteBuffer createUserIDPkt(int id) {
    	byte[] bytes = new byte[1 + 1*4];
    	ByteBuffer buffer = ByteBuffer.wrap(bytes);
    	
        buffer.put((byte) OPCODES.INFORM_USER_ID_CODE.ordinal());
    	buffer.putInt(id);
    	
    	buffer.flip();
        return buffer;
    }
	
    public static ByteBuffer createShipCreationPkt(ServerShip s) {
    	byte[] bytes = new byte[1 + 2*4 + 2*8 + 1*4];
    	ByteBuffer buffer = ByteBuffer.wrap(bytes);
    	
        buffer.put((byte) OPCODES.CREATE_SHIP_CODE.ordinal());

    	buffer.putInt(s.getGuid());
    	buffer.putInt(s.getTypeid());
    	
    	buffer.putDouble(s.getX());
    	buffer.putDouble(s.getY());
    	
    	buffer.putInt(s.getOwnerID());
    	
        buffer.flip();
        return buffer;
    }
    
    public static ByteBuffer createShipMovePkt(Ship s) {
    	byte[] bytes = new byte[1 + 1*4 + 4*8];
    	ByteBuffer buffer = ByteBuffer.wrap(bytes);
    	
        buffer.put((byte) OPCODES.MOVE_SHIP_CODE.ordinal());

    	buffer.putInt(s.getGuid());

    	buffer.putDouble(s.getX());
    	buffer.putDouble(s.getY());
    	
    	buffer.putDouble(s.getVelocity());
    	buffer.putDouble(s.getHeading());
    	
        buffer.flip();
        return buffer;
    }
    
    public static OrderMoveShip decodeShipMoveOrder(ByteBuffer b) {
    	int guid = b.getInt();
    	double x = b.getDouble();
    	double y = b.getDouble();
    	return new OrderMoveShip(guid, x, y);
    }
    
    public static ByteBuffer createTurretFirePkt(int shooterGuid, int shooterTurretID, int targetGuid, double offsetlength, double offsetwidth, double damage) {
    	byte[] bytes = new byte[1 + 3*4 + 2*8 + 1*8];
    	ByteBuffer buffer = ByteBuffer.wrap(bytes);
    	
        buffer.put((byte) OPCODES.FIRE_TURRET_CODE.ordinal());

    	buffer.putInt(shooterGuid);
    	buffer.putInt(shooterTurretID);
    	buffer.putInt(targetGuid);

    	buffer.putDouble(offsetlength);
    	buffer.putDouble(offsetwidth);
    	
    	buffer.putDouble(damage);
    	
    	buffer.flip();
        return buffer;
    }
}
